<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="3" :labelWidth="100" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">关 闭</el-button>
      <AjaxButton v-if="type !== 'show'" type="primary" :click="saveDialog">{{ btnText }}</AjaxButton>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { isAuthorized, notifyAction } from '@/utils/index';
// import { addBrand, updateBrand } from '@/api/common/cars/brandcarsModels';
import { modifyPartCostPrice } from '@/api/part/inventoryManager/partCostAdjust';

export default {
  name: 'editPage',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '保 存' };
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      if (this.type === 'edit') {
        // debugger;
        // 更新接口需要的记录 ID
        // console.log(this.rowData.COST_AMOUNT);

        this.partNo = this.rowData.PART_NO;
        this.storageCode = this.rowData.STORAGE_CODE;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '仓库代码',
          fieldName: 'STORAGE_CODE',
          hidden: true,
          disabled: true
          // rules: [{ required: true,message: '请输入仓库代码', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '仓库名称',
          fieldName: 'STORAGE_NAME',
          disabled: true
          // rules: [{ required: true,message: '请输入仓库代码', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'PART_NO',
          disabled: true
          // rules: [{ required: true,message: '请输入配件代码',  trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'PART_NAME',
          disabled: true
          // rules: [{ required: true,message: '请输入配件名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '原成本单价',
          fieldName: 'COST_PRICE',
          precision: 2,
          min: 0,
          disabled: true,
          onInput: props => {
            console.log(props, kkkks);
          }
          // rules: [{ required: true, message: '请输入原成本单价', trigger: 'blur' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '数量',
          fieldName: 'STOCK_QUANTITY',
          precision: 2,
          disabled: true
          // rules: [{ required: true, message: '请输入数量', trigger: 'blur' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '原成本金额',
          fieldName: 'COST_AMOUNT',
          precision: 2,
          disabled: true,
          initialValue: this.rowData.COST_AMOUNT,
          min: 0
          // rules: [{ required: true, message: '请输入原成本金额', trigger: 'blur' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '新成本单价',
          fieldName: 'NEW_PRICE',
          precision: 2,
          change: val => {
            this.calMoney(val);
          },
          rules: [{ required: true, message: '请输入新成本单价', trigger: 'blur' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '新成本金额',
          fieldName: 'NEW_AMOUNT',
          readonly: true,
          disabled: true,
          precision: 2,
          rules: [{ required: true, message: '请输入新成本金额', trigger: 'blur' }]
        }
        // {
        //   type: 'INPUT',
        //   label: '品牌代码',
        //   fieldName: 'brandCode',
        //   placeholder: '请输入...',
        //   rules: [{ required: true, message: '请输入品牌代码', trigger: 'blur' }]
        // },
        // {
        //   type: 'INPUT',
        //   label: '品牌名称',
        //   fieldName: 'brandName',
        //   placeholder: '请输入...',
        //   rules: [{ required: true, message: '请输入品牌名称', trigger: 'blur' }]
        // },
        // {
        //   type: 'SELECT',
        //   label: '是否有效',
        //   fieldName: 'isValid',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('1004'),
        //   rules: [{ required: true, message: '请选择有效性', trigger: 'change' }]
        // },
        // {
        //   type: 'SELECT',
        //   label: '数据来源',
        //   fieldName: 'dataSources',
        //   placeholder: '请选择',
        //   itemList: [{ value: 10451001, text: '店面' }, { value: 10451003, text: '厂家' }],
        //   rules: [{ required: true, message: '请选择数据来源', trigger: 'change' }]
        // },
        // {
        //   type: 'SELECT',
        //   label: '是否整车销售',
        //   fieldName: 'isValid',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('1004'),
        //   rules: [{ required: true, message: '请选择整车销售', trigger: 'change' }]
        // }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'add') {
          res = await addBrand(this.saveData);
        }
        if (this.type === 'edit') {
          // console.log(this.saveData, '-----saveData-----');
          this.saveData.STORAGE_NAME = this.rowData.STORAGE_NAME;
          res = await modifyPartCostPrice(this.saveData, this.partNo, this.storageCode);
          // console.log(res);
          if (res.resultCode === 200) {
            notifyAction('成本价修改成功', 'success');
            this.closeDialog('refresh');
          }
          // if (isAuthorized('1313', '13131003')) {

          // } else {
          //   this.$notify({
          //     title: '警告',
          //     message: '您无权限修改成本价！',
          //     type: 'warning'
          //   });
          // }
          // res = await test();
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    calMoney(val) {
      console.log('change::', val);
      let num = this.formList[5].initialValue;
      console.log('num::', num);
      this.formList[8].initialValue = (val * num).toFixed(2);
      this.formList = [...this.formList];
    }
  }
};
</script>

<style lang="less" scoped></style>
